/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  net.babeuf.form.samples
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import net.babeuf.form.conditions.MatchCondition;
	import net.babeuf.form.errors.FieldError;
	import net.babeuf.form.fields.input.EmailField;
	import net.babeuf.form.fields.input.InputField;
	import net.babeuf.form.fields.input.PasswordField;
	import net.babeuf.form.Form;
	import net.babeuf.form.sensors.TextFieldColorSensor;
	
	/**
	 * First sample of AS3 Form use
	 * @author Damien Pegoraro
	 */
	public class BasicSample extends Sprite 
	{
		[Embed(source='assets/formdemo.swf', symbol='formSample1')]
		private static var FORM_VIEW : Class ; 
				
		private var view : Sprite ; 
		private var form : Form ; 
		
		
		public function BasicSample() 
		{			
			buildView() ; 	
			buildForm() ; 	
		}
		
				
		/**
		 * Add the form view to the scene.
		 * The view is designed in Flash IDE and not generated by the API
		 */
		private function buildView():void 
		{
			
			view = new FORM_VIEW() ; 
			view.x = view.y = 10 ; 
			addChild(view) ; 
		}
		
		/**
		 * Form construction
		 */
		private function buildForm():void 
		{
			// create a new Form
			form = new Form() ; 		
			
			// errors messages declaration. 
			// this can also be done with a XML Object 
			form.messager.add("email_empty", "Your email is empty") ; 
			form.messager.add("email_invalid", "Your email is invalid") ; 
			form.messager.add("password_too_small", "Your password is too small") ; 
			form.messager.add("password_empty", "Your password is empty") ; 
			form.messager.add("password_confirm_empty", "You must confirm your password") ; 
			form.messager.add("password_confirm_too_small", "Your password confirm is too small") ; 
			form.messager.add("password_confirm_invalid", "Password and confirmation does not match") ; 
			
			
			// declare optional name field
			form.addField( new InputField( view["tfName"] ), "name" ) ; 
			
			// setting label into the input field.
			InputField(form.getField("name")).setDefaultLabel("Type your name (not required)") ; 
			
			// make this field optional
			form.getField("name").optional = true ; 
						
			// declaring email field and setting errors 
			// invalid email error is specific so casting is required
			form.addField( new EmailField( view["tfEmail"] ), "email" ) ; 
			form.getField("email").setEmptyError("email_empty") ; 
			EmailField(form.getField("email")).setInvalidMailError("email_invalid") ; 
			
			// setting label into the input field.
			InputField(form.getField("email")).setDefaultLabel("Type your email*...") ; 
			
			// adding a basic sensor to show the error 
			form.getField("email").addSensor( new TextFieldColorSensor( view["tfEmail"] , 0xFF0000 ) ) ; 
			
			// declaring password field and setting errors
			form.addField( new PasswordField( view["tfPassword"], 6), "password") ; 
			form.getField("password").setEmptyError("password_empty") ; 
			form.getField("password").setMinSizeError("password_too_small") ; 
			InputField(form.getField("password")).setDefaultLabel("Type your password*...") ; 
			form.getField("password").addSensor( new TextFieldColorSensor( view["tfPassword"] , 0xFF0000 ) ) ; 
			
			// password confirmation is just another password field
			form.addField( new PasswordField( view["tfPasswordConfirm"], 6), "confirm") ; 
			form.getField("confirm").setEmptyError("password_confirm_empty") ; 
			form.getField("confirm").setMinSizeError("password_confirm_too_small") ; 
			InputField(form.getField("confirm")).setDefaultLabel("Confirm your password*...") ; 
			form.getField("confirm").addSensor( new TextFieldColorSensor( view["tfPasswordConfirm"] , 0xFF0000 ) ) ; 
			
			// As we don't want this value to be saved, let's say not to export it.
			form.getField("confirm").notSent = true ; 
	
			// Add an extra condition the set de password / password confirm match rule
			form.addCondition( new MatchCondition( form.getField( "password"), form.getField("confirm")), "match", "password_confirm_invalid") ; 

			// Add sensors to this condition
			form.getCondition("match").addSensor( new TextFieldColorSensor( view["tfPassword"] , 0xFF0000 ) ) ;
			form.getCondition("match").addSensor( new TextFieldColorSensor( view["tfPasswordConfirm"] , 0xFF0000 ) ) ;

			// empty form and reset labels
			form.empty() ; 

			// and let's link the OK button  :
			SimpleButton(view.getChildByName("btnOk")).addEventListener(MouseEvent.CLICK, validateForm ) ; 			
		}
			
		/**
		 * Validate form data 
		 * Error is displayed if necessary
		 * Values are taken
		 */
		private function validateForm(e:MouseEvent):void 
		{
			// validate the form values. The param tells how many errors you want on a validate ( 0 for all errors ) 
			var errors : Array = form.validate(1) ; 
			
			// no errors, form is ok
			if ( errors.length == 0 )
			{
				// get form values : 
				// if includeEmpty param is set to true, optional fields left blank will also be writen to the object
				var values:  Object = form.values() ; 
				
				// write result into error field
				view["tfError"].text = "Values OK :" ; 
				
				for ( var i : String in values ) 
				{
					view["tfError"].appendText( "\n"  + i + " : " + values[i] ) ; 
				}
			}
			else
			{
				// get the first returned error
				var error : FieldError = errors[0] ; 
				
				// show the message in the error field : 
				view["tfError"].text = error.message ; 			
			}			
		}		
	}
}